// newHousePackage/pages/myHouseResource/myHouseResource.js
import { queryMyHouseResource, submitExamineHouseRequest, onOrOffHouse, deleteOneHouse } from '../../../../service/house_service'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    houseList: [

    ],

    page: 0,
    pageSize: 10,
    searchWord: "",
    scrollRefreshStatus: true,
    isShowLoading: false,
    isBottomShow: false,
    showSuccessPop: false,
    switchArr: [
      {
        title: "全部",
        key: "-2",
      },
      {
        title: "未提交",
        key: 0,
      },
      {
        title: "审核中",
        key: 1
      },
      {
        title: "审核成功",
        key: 2
      },
      {
        title: "审核失败",
        key: 3
      }
    ],
    exStatus: 0,//审核状态
    examineStatus: "2",//默认审核成功
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryMyHouseResource();
  },
  //切换房源对应状态
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
      exStatus: index,
      examineStatus: item.key,
      page: 0,
      searchWord: "",
      isBottomShow: false,
      houseList: []
    })
    this.queryMyHouseResource();

  },
  //获取管理的房源列表
  queryMyHouseResource() {
    wx.showLoading();
    let pageNum = this.data.page;
    let pageSize = this.data.pageSize;
    queryMyHouseResource(this.data.page, this.data.pageSize, this.data.searchWord, this.data.examineStatus).then(res => {
      this.setData({
        scrollRefreshStatus: false,
      })
      console.log("JJLog:房源列表:" + JSON.stringify(res));
      wx.hideLoading();
      if (res.code == 200) {
        let tempArr = [];
        tempArr = this.data.houseList;
        if (tempArr.length > 0 && pageNum > 0) {
          //加载下一页:
          tempArr = tempArr.concat(res.data);
          if (res.data.length < pageSize) {
            this.setData({
              isShowLoading: false,
              isBottomShow: true,
            })
            this.setData({
              houseList: tempArr
            })
          } else if (res.data.length == pageSize) {
            this.setData({
              isShowLoading: false,
            })
            this.setData({
              houseList: tempArr
            })
          }
        } else {
          this.setData({
            isShowLoading: false,
          })
          this.setData({
            houseList: res.data
          })
          if (res.data.length < pageSize) {
            this.setData({
              isBottomShow: true
            })
          }
        }
      }
    }).catch(err => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false,
      })
      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },


  //去到房源列表页面:
  pushToDetail(e) {
    let item = e.currentTarget.dataset.item;

    wx.navigateTo({
      url: '/formPackage/pages/commissionManager/houseCommissionList/houseCommissionList?houseNumber=' + item.houseNumber + "&isCloseReport=" + item.isCloseReport,
    })
  },

  //发布新房:
  pushNewHouse() {
    wx.navigateTo({
      url: '/formPackage/pages/newHousePublishModule/newHousePublishFirst/newHousePublishFirst',
    })
  },





  searchAction(e) {
    this.setData({
      searchWord: e.detail.searchWord,
      page: 0,
      isBottomShow: false,
    })
    this.queryMyHouseResource();

  },



  //滚动到底部
  scrolltolower() {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.page + 1;
    this.setData({
      page: tempPage,
      isShowLoading: true,
    })
    this.queryMyHouseResource();
  },
  //下拉刷新:
  refreshDownData() {
    this.setData({
      scrollRefreshStatus: true,
      page: 0,
      isBottomShow: false,
    })
    this.queryMyHouseResource();
  },


  //提交审核操作:
  submitExamineAction(e) {
    let houseNumber = e.currentTarget.dataset.number;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定提交房源信息去审核？',
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.submitExamineHouseRequest(houseNumber, index)
        }
      }
    })

  },
  submitExamineHouseRequest(houseNumber, index) {
    wx.showLoading();
    submitExamineHouseRequest(houseNumber).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          showSuccessPop: true
        })
        let arr = this.data.houseList;
        arr.splice(index, 1);
        this.setData({
          houseList: arr
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:submitExamineHouseRequest", err);
    })
  },
  successPopSure() {
    this.setData({
      showSuccessPop: false
    })
  },

  //上架或者下架操作:
  upOrDownClick(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    let isPush = item.isPush;
    let remindText = "是否下架当前房源";
    if (!isPush) {
      remindText = "是否上架当前房源"
    }
    wx.showModal({
      title: '提示',
      content: remindText,
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.onOrOffHouseRequest(item.houseNumber, index, isPush)
        }
      }
    })


  },
  //上架下架请求:
  onOrOffHouseRequest(houseNumber, index, isPush) {
    wx.showLoading();
    onOrOffHouse(houseNumber).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        let successText = "下架成功";
        if (!isPush) {
          successText = "上架成功"
        }
        wx.showToast({
          title: successText,
          icon: "success"
        })
        let item = 'houseList[' + index + '].isPush';
        this.setData({
          [item]: !isPush,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:onOrOffHouse", err);
    })
  },


  //删除一个房源;
  rightCancelBtnAction(e) {
    let index = e.currentTarget.dataset.index;
    let houseNumber = e.currentTarget.dataset.number;
    wx.showModal({
      title: '提示',
      content: "是否删除房源,删除后无法恢复",
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.deleteOneHouseRequest(houseNumber, index);
        }
      }
    })
  },

  deleteOneHouseRequest(houseNumber, index) {
    wx.showLoading();
    deleteOneHouse(houseNumber, index).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "删除成功",
          icon: "success"
        })
        let arr = this.data.houseList;
        arr.splice(index, 1);
        this.setData({
          houseList: arr
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:deleteOneHouse", err);
    })
  },

  //重新编辑房源:
  editFailHouseAction(e) {

  },

  publishDynAction(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/newHousePackage/pages/publishHouseDyn/publishHouseDyn?houseName=' + item.name + "&houseNumber=" + item.houseNumber,
    })
  },



  //新增佣金
  addNewCommissionAction(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/formPackage/pages/commissionManager/addNewCommission/addNewCommission?houseNumber=' + item.houseNumber + "&isCloseReport=" + item.isCloseReport,
    })
  },


})